<?php

class ToolAction extends Action{
	//������ģ�͵��ֶ�д�뻺��
	function model(){
		C('DB_FIELDS_CACHE',TRUE);
		$tables = D('Agent')->query('show tables');
		foreach ($tables as $v) {
			$tableName = current($v);
			$preFix = C('DB_PREFIX');
			if ($preFix) {
				if (0===strpos($tableName, $preFix)) {
					$tableName = substr($tableName, strlen($preFix));
				}else {
					continue;
				}
			}
			M(ucfirst($tableName));
		}
		$files = glob(LIB_PATH.'Model/*Model.class.php');
		foreach ($files as $file){
			$fileName = basename($file);
			$modelName = substr($fileName, 0,-15);
			$fields = F('_fields/'.C('DB_NAME').".$modelName");
			if ($fields) {
				$modelContent = file_get_contents($file);
				$endContent = '';
				$fieldsStr = 'protected $fields = '.substr(file_get_contents(DATA_PATH.'_fields/'.C('DB_NAME').".$modelName.php"), 13,-3);
				$fieldsStr = preg_replace('# \d+ => #', '',  $fieldsStr);
				if (strpos($modelContent,'protected $fields')) {
					$endContent = preg_replace('#protected \$fields.+;#', $fieldsStr, $modelContent);
				}else {
					$endContent = preg_replace('#extends Model\{#', "extends Model{ \n\t$fieldsStr", $modelContent);
				}
				if ($endContent!=$modelContent) {
					if(!file_put_contents($file, $endContent)){
						echo $modelName;
					}
				}
			}
		}
	}
}

